store = require 'store'
newId = require("uuid-pure").newId


I = store.get('I')
if not I
    I = newId(12,64)
    store.set('I',I)

logout = ->
    I = I.slice(0,12)
    store.set('I',I)
    return

module.exports = {
    I
    logout
    user:->
        if not ('USER' of window)
            if I.length < 24
                user = 0
            else
                user = await $.getJSON("*auth/user/meta")
                if not user
                    logout()
            window.USER = user
        return USER

    login:->
        I = I.slice(0,12)+newId(12,64)
        store.set('I',I)
        module.exports.I = I
        return I
}

